import logging

from selenium.webdriver.support.wait import WebDriverWait

from utils import DriverUtils


class BuyerBasePage:
    def __init__(self):
        self.driver = DriverUtils.get_buyer_driver()

    # 公用元素定位方法
    # 调用该定位时，使用显示等待的方式定位元素
    # 记录代码执行定位的日志

    def find_el(self, location):
        try:
            # 显示等待定位元素
            el = WebDriverWait(self.driver, 10, 1).until(lambda x: x.find_element(*location))
            logging.info(f"execute find_el {location} success!")
        except Exception as e:
            logging.error(f"execute find_el {location} failed!")
            el = None
        # 返回元素对象
        return el

    # 公用模拟输入
    def input_text(self, el, key_text):
        try:
            # 清空输入框
            el.clear()
            # 输入文本
            el.send_keys(key_text)
            logging.info(f"input {key_text} success!")
        except Exception as e:
            logging.error(f"input {key_text} failed")

    # frame切换
    def switch_frame(self, i_el):
        try:
            self.driver.switch_to.frame(i_el)
            logging.info("switch iframe success!")
        except Exception as e:
            logging.error(f"switch iframe failed!")

    # 窗口切换
    def switch_window(self, n):
        try:
            # 1.获取所有句柄信息
            handles = self.driver.window_handles
            # 2.切换到指定窗口
            self.driver.switch_to.window(handles[n])
            logging.info(f"switch window success!")
        except Exception as e:
            logging.error(f"switch window failed!")


class AdminBasePage:
    def __init__(self):
        self.driver = DriverUtils.get_admin_driver()

    # 公用元素定位方法
    # 调用该定位时，使用显示等待的方式定位元素
    # 记录代码执行定位的日志

    def find_el(self, location):
        try:
            # 显示等待定位元素
            el = WebDriverWait(self.driver, 10, 1).until(lambda x: x.find_element(*location))
            logging.info(f"execute find_el {location} success!")
        except Exception as e:
            logging.error(f"execute find_el {location} failed!")
            el = None
        # 返回元素对象
        return el

    # 公用模拟输入
    def input_text(self, el, key_text):
        try:
            # 清空输入框
            el.clear()
            # 输入文本
            el.send_keys(key_text)
            logging.info(f"input {key_text} success!")
        except Exception as e:
            logging.error(f"input {key_text} failed")

    # frame切换
    def switch_frame(self, i_el):
        try:
            self.driver.switch_to.frame(i_el)
            logging.info("switch iframe success!")
        except Exception as e:
            logging.error(f"switch iframe failed!")

    # 窗口切换
    def switch_window(self, n):
        try:
            # 1.获取所有句柄信息
            handles = self.driver.window_handles
            # 2.切换到指定窗口
            self.driver.switch_to.window(handles[n])
            logging.info(f"switch window success!")
        except Exception as e:
            logging.error(f"switch window failed!")


class AppBasePage:
    def __init__(self):
        self.driver = DriverUtils.get_app_driver()

    # 公用元素定位方法
    # 调用该定位时，使用显示等待的方式定位元素
    # 记录代码执行定位的日志

    def find_el(self, location):
        try:
            # 显示等待定位元素
            el = WebDriverWait(self.driver, 10, 1).until(lambda x: x.find_element(*location))
            logging.info(f"execute find_el {location} success!")
        except Exception as e:
            logging.error(f"execute find_el {location} failed!")
            el = None
        # 返回元素对象
        return el

    # 公用模拟输入
    def input_text(self, el, key_text):
        try:
            # 清空输入框
            el.clear()
            # 输入文本
            el.send_keys(key_text)
            logging.info(f"input {key_text} success!")
        except Exception as e:
            logging.error(f"input {key_text} failed")

    # frame切换
    def switch_frame(self, i_el):
        try:
            self.driver.switch_to.frame(i_el)
            logging.info("switch iframe success!")
        except Exception as e:
            logging.error(f"switch iframe failed!")

    # 窗口切换
    def switch_window(self, n):
        try:
            # 1.获取所有句柄信息
            handles = self.driver.window_handles
            # 2.切换到指定窗口
            self.driver.switch_to.window(handles[n])
            logging.info(f"switch window success!")
        except Exception as e:
            logging.error(f"switch window failed!")
